/**
 * @author caif
 * @fileoverview document
 */
;(function(){
	window.tpl =  window.tpl || {};
	window.App = window.App || {};
	$('#glodon_error_dlg').modal({
    	keyboard: true,
    	show: false,
    	backdrop: "static"
    });
	
	window.App.Models = {},
	window.App.Views = {},
	window.App.Routers = {},
	window.App.Collections = {},
	window.App.viewInstances = {},
	window.App.initialize = function(option , isRouter){
		if(isRouter){
			this.viewInstances.appView = new App.Views.appView;
			this.router = new App.Routers[option];
			Backbone.history.start();
		}else{
			this.main = new App.Views[option];
		}
	};
	
	$.ajaxSetup({
	    complete : function( xhr, textStatus, errorThrown ){
	        if( xhr.responseText == "logout" && xhr.status  == 401){
	            var index = window.location.protocol + "//" + window.location.host + window._global.contextPath + "/index";
	            if( window.location.href !=  index)
	            {
	                window.location.href = index;
	            }
	        }
	    }
	});
	$(document).ajaxError(function(e, xhr, ajaxSettings, thrownError){
	   App.util.actionTip(xhr.responseText);
	});
	//i18n
    App.i18n = {
        get: function(){
        	if(_.isArray(arguments[1])){
        	   return this.format(this.get(arguments[0]), arguments[1]);
        	}
        	var key = App.i18nMessages[arguments[0]];
            return key ? key.message : arguments[0];
        },
        getMessage: function(){
           return this.get.apply(this, arguments);
        },
        getMessages: function(keyArr){
            var messages = [];
            for(var i = 0, l = keyArr.length; i < l; i++){
                messages.push(this.get(keyArr[i]));
            }
            return messages;
        },
        format: function(str, arr){
        	var strArr = str.split('\\$');
            for(var i = 0, l = arr.length, regExp; i < l; i++){
                regExp = new RegExp('\\$' + i, 'g');
                _.each(strArr, function(s, idx){
                    strArr[idx] = s.replace(regExp, arr[i]); 
                });
            }
            return strArr.join('\$');
        }
    }
})()
